<template>
  <div class="login-container">
    <el-form
      class="login-form"
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      @keyup.enter="handlerLogin">
        <div class="title-container">
            <h3 class="title">用户登录</h3>
        </div>
        <el-form-item prop="username">
            <el-input
                :prefix-icon="Avatar"
                placeholder="输入用户名"
                name="username"
                type="text"
                v-model="loginForm.username" />    
        </el-form-item>
        <el-form-item prop="password">
            <el-input
                :prefix-icon="Lock"
                show-password
                placeholder="请输入密码"
                name="password"
                v-model="loginForm.password" />
        </el-form-item>
        <el-button
            type="primary"
            class="login-btn"
            :loading="loading"
            @click="handlerLogin">
            登录
        </el-button>
    </el-form>
  </div>
</template>

<script setup>
// 表单检验 有三个要求：
/**
 * 1、为 el-form 绑定 model 属性
 * 2、为 el-form 绑定 rules 属性
 * 3、为 el-form-item 绑定 prop 属性
 */
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Avatar, Lock } from '@element-plus/icons-vue'
import { userStore } from '@/store/user'
import { useRouter } from 'vue-router'
const router = useRouter()
const useUserStore = userStore()

// 登录方法要用到的参数
const loginForm = ref({
    username: 'admin',
    password: 'admin'
})
// 验证规则
const loginRules = ref({
  username: [
    {
      required: true,
      trigger: 'blur',
      message: '用户名不能为空'
    }
  ],
  password: [
    {
      required: true,
      trigger: 'blur',
      validator: (_, value) => {
        return new Promise((resolve, reject)=> {
          if (value.length < 5) {
            reject(new Error('密码不能少于5位'))
          } else {
            resolve()
          }
        })
      }
    }
  ]
})

// 处理登录 要给loginFormRef 赋个空值
const loading = ref(false)
const loginFormRef = ref() // 赋值后，它会去找DOM中Ref的属性值
const handlerLogin = () => {
    // 1. 进行表单校验
    loginFormRef.value.validate((valid) => {
    if (!valid) return
    // 2. 触发登录动作
    // console.log('表单值',loginForm.value)
    loading.value = true
    useUserStore.getLogin(loginForm.value)
        .then(() => {
            router.push({ path: '/home' })
            loading.value = false
        }).catch((err) => {
            ElMessage.error(`登录失败: ${err.message}`)
            loading.value = false
        })
  })
}
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;

    :v-deep(.el-form-item) {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    :v-deep(.el-input) {
      display: inline-block;

      .el-input__wrapper {
        width: 100%;
        border: none;
        box-shadow: none;
        background-color: transparent;
      }

      input {
        background: transparent;
        border: 0px;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
      }
    }
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
}
.login-btn {
  height: 49px;
  width: 100%;
}
</style>
